/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package p2a;

import java.util.Random;
import java.util.Vector;

/**
 *
 * @author user
 */
public class CTest {
    
        public Vector<TestCase> generateTestCasesFromRules(Vector<Rule> Rs) {
        Vector<TestCase> testCases = new Vector();
        
        for(int i=0; i< Rs.size(); i++)
        {
            testCases.addAll(generateTestCasesFromRule(Rs.get(i)));
        }
        return testCases;
    }

    public Vector<TestCase> generateTestCasesFromRule(Rule R) {
        Vector<TestCase> testCases = new Vector();

        String[] flags = {"RST", "FIN", "SYN/ACK", "SYN"};
        for (int id = 0; id < 4; id++) {
            TestCase test_case = new TestCase();
            test_case.bind(R);
            test_case.setSrcIp(IP(R.getSrcIp()));
            test_case.setSrcPort(PORT(R.getSrcPort()));
            test_case.setDestIp(IP(R.getDestIp()));
            test_case.setDestPort(PORT(R.getDestPort()));
            test_case.setFlag(flags[id]);
            if (id == 2) {
                test_case.switchIPs();
            }

            testCases.add(test_case);

        }
        echoTestCases(testCases);
        return testCases;
    }

    private void echoTestCases(Vector<TestCase> testCases) {
        for (int idx = 0; idx < testCases.size(); idx++) {
            System.out.println("TC_" + idx + ":" + testCases.get(idx).toString());
        }
    }

    private String IP(String ip) {
        if (ip.equals("*.*.*.*") || ip.equalsIgnoreCase("any")) {
            String[] muIP = {"202.212.5.30", "216.239.33.96", "131.107.137.165"};
            return muIP[new Random().nextInt(2)];
        } else if (ip.indexOf("*") >= 0) {

            String masks[] = {"/8", "/16", "/24"};
            String mask = masks[3 - Helper.getCharCount(ip, '*')];
            String cidr = ip.replace("*", "0").concat(mask);
            SubnetUtils su = new SubnetUtils(cidr);
            String[] ips = su.getInfo().getAllAddresses();
            return ips[new Random().nextInt(ips.length - 1)];
        }
        return ip;
    }

    private String PORT(String port) {

        if (port.toUpperCase().equals("ANY")) {
            return String.valueOf(new Random().nextInt(1024));
        }
        return port;
    }
}
